#!/usr/bin/python
# -*- coding: utf-8 -*-

import multiprocessing
import sys
import time


def myprint(obj):
    sys.stdout.write(str(obj) + '\n')


def exit_error():
    sys.exit(1)


def exit_ok():
    return


def return_value():
    return 1


def raises():
    raise RuntimeError('There was an error!')


def terminated():
    time.sleep(3)


if __name__ == '__main__':
    jobs = []
    for f in [exit_error, exit_ok, return_value, raises, terminated]:
        try:  # for python2.x
            myprint('Starting process for %s' % f.func_name)
            j = multiprocessing.Process(target=f, name=f.func_name)
        except AttributeError:  # for python3.x
            myprint('Starting process for %s' % f.__name__)
            j = multiprocessing.Process(target=f, name=f.__name__)
        jobs.append(j)
        j.start()

    jobs[-1].terminate()

    for j in jobs:
        j.join()
        myprint('%s.exitcode = %s' % (j.name, j.exitcode))
